//
//  RecordViewController.m
//  WayToHome
//
//  Created by A-01 on 2020/6/22.
//  Copyright © 2020 A-01. All rights reserved.
//

#import "RecordViewController.h"
#import "MyRecordTableViewCell.h"

@interface RecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UILabel * titleLab;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * dataArr;

@end

@implementation RecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self loadData];
}


- (void)loadData{
    
    [self.dataArr removeAllObjects];

    NSArray * arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"];
    
    for (NSString * key in arr) {
        
        NSString * value = DEF_GET_OBJECT(key);
        if (!value && value.length == 0) {
            
            value = @"no record";
        }
        NSDictionary * dict = @{@"level":key,@"value":value};
        [self.dataArr addObject:dict];
    }
    
    [self.tableView reloadData];
}

- (void)setupUI{
            
    UILabel * titleLab = [UILabel new];
        
    titleLab.text = @"My Record";
    titleLab.textColor = [UIColor colorWithHexString:@"0AB966"];
    titleLab.font = [UIFont boldSystemFontOfSize:24];
    titleLab.textAlignment = NSTextAlignmentCenter;
            
    self.titleLab = titleLab;
            
    [self.view addSubview:self.titleLab];

    UITableView * tableView = [[UITableView alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];//ColorWithRGB(0xEFF0F1);
    tableView.rowHeight = 50;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.tableView = tableView;
    
    [self.tableView registerClass:[MyRecordTableViewCell class] forCellReuseIdentifier:@"MyRecordTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    [self addImageView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyRecordTableViewCell" forIndexPath:indexPath];

    if (indexPath.row == 0) {
        
        cell.titleLab.text = @"level";
        cell.subLab.text = @"Time";
        cell.subLab.textColor = [UIColor colorWithHexString:@"0AB966"];
    }else{
        
        NSDictionary * dict = self.dataArr[indexPath.row - 1];
        cell.titleLab.text = dict[@"level"];
        cell.subLab.text = dict[@"value"];
        cell.subLab.textColor = [UIColor colorWithHexString:@"E99C48"];
    }
    

    return cell;
}


- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];

    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(15);
    }];
       
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(15);
        make.bottom.mas_equalTo(self.view).offset(-135);
        make.leading.trailing.mas_equalTo(self.view);
    }];
}


#define Start_X          50.0f      // 第一个按钮的X坐标
#define Start_Y          60.0f     // 第一个按钮的Y坐标
#define Height_Space     12.0f     // 竖间距
#define Button_Width    38.0f    // 宽
-(void)addImageView
{
    for (int i = 0 ; i < 5; i++) {

        NSString *img;
        if (i % 2==0) {
            
            img = @"flower";
        }else{
            
            img = @"leaf";
        }
        // 圆角按钮
        UIImageView *imageView = [UIImageView new];
        imageView.image = [UIImage imageNamed:img];
        imageView.frame = CGRectMake(Start_X, i * (Button_Width + Height_Space)+Start_Y, Button_Width, Button_Width);
         
        [self.view addSubview:imageView];
    }
}


- (NSMutableArray *)dataArr{
    
    if (!_dataArr) {
        
        _dataArr = [[NSMutableArray alloc] init];
    }
    
    return _dataArr;;
}


@end
